package com.example.xcpeekaboo.listeners;

import com.example.xcpeekaboo.game.DisguiseManager;
import com.example.xcpeekaboo.game.GameManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;

public class GameListener implements Listener {
    private final GameManager gameManager;
    private final DisguiseManager disguiseManager;

    public GameListener() {
        this.gameManager = GameManager.getInstance();
        this.disguiseManager = DisguiseManager.getInstance();
    }

    @EventHandler
    public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
        Player player = event.getPlayer();
        // 只有当玩家是游戏参与者时才限制命令
        if (gameManager.isSeeker(player) || gameManager.isHider(player)) {
            String command = event.getMessage().substring(1).split(" ")[0].toLowerCase();
            if (!gameManager.isCommandAllowed(command)) {
                event.setCancelled(true);
                player.sendMessage("§c游戏进行中，只允许使用退出指令！");
            }
        }
    }

    @EventHandler
    public void onPlayerDamage(EntityDamageByEntityEvent event) {
        if (!(event.getEntity() instanceof Player) || !(event.getDamager() instanceof Player)) {
            return;
        }

        Player victim = (Player) event.getEntity();
        Player attacker = (Player) event.getDamager();

        if (!gameManager.isGameRunning()) {
            return;
        }

        // 只有当受害者是寻找者时才取消伤害
        if (gameManager.isSeeker(victim)) {
            event.setCancelled(true);
            return;
        }

        if (gameManager.isSeeker(attacker) && gameManager.isHider(victim)) {
            if (gameManager.isPlayerAlive(victim)) {
                gameManager.setPlayerCaught(victim);
                disguiseManager.undisguisePlayer(victim);
                victim.setGameMode(org.bukkit.GameMode.SPECTATOR);
                victim.sendMessage("§c你被抓到了！现在进入旁观者模式");
                attacker.sendMessage("§a你抓到了一个躲藏者！");
            }
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        if (gameManager.isGameRunning()) {
            gameManager.removePlayer(player);
            disguiseManager.undisguisePlayer(player);
        }
    }

    @EventHandler
    public void onPlayerDeath(org.bukkit.event.entity.PlayerDeathEvent event) {
        Player player = event.getEntity();
        
        if (gameManager.isGameRunning() && gameManager.isHider(player) && gameManager.isPlayerAlive(player)) {
            // 非正常死亡时将躲藏者标记为被抓获
            gameManager.setPlayerCaught(player);
            disguiseManager.undisguisePlayer(player);
            player.setGameMode(org.bukkit.GameMode.SPECTATOR);
            player.sendMessage("§c你因非正常死亡被抓获！现在进入旁观者模式");
            player.getServer().broadcastMessage("§e一名躲藏者因非正常死亡被抓获！");
        }
    }
}